<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * Copyright (C) 2007  Robert Moss
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

class Match extends JLClass{
  
  var $match_id;
  var $match_number;
// new for date by match Gucky 2007/05/25
  var $match_date;
// new for date by match Gucky 2007/05/25 ends
  var $project_id;
  var $round_id;
  var $matchpart1;
  var $matchpart2;
  var $matchpart1_result;
  var $matchpart2_result;
// new for period results Gucky 2007/05/25
	var $matchpart1_result_split;
  var $matchpart2_result_split;
  var $matchpart1_legs;
  var $matchpart2_legs;
// new for period results Gucky 2007/05/25 ends
  var $match_result_type; //0=normal,1=addtime,2=penalty
  var $match_result_detail;
  var $alt_decision;
  var $matchpart1_result_decision;
  var $matchpart2_result_decision;
  var $decision_info;
  var $count_result; //boolean
  var $published;
  var $playground_id;
  var $referee_id;
  var $crowd;
  var $checked_out;
  var $checked_out_time;
  var $summary;
  var $homeroster;
  var $awayroster;
  var $show_report;
  
  function Match( &$db ) {
		$this->mosDBTable( '#__joomleague_matches', 'match_id', $db );
	}
// function save_array changed for date per match and period results
// Gucky 2007/05/25
  function save_array( $cid=null, $post=null, $zusatz=false){
    $datatable[0] = '#__joomleague_matches';
	$fields = $this->_db->getTableFields( $datatable );
	foreach($fields as $field) {
		$query = '';
		$datafield = array_keys($field);
		if ($zusatz) $fieldzusatz = $cid;
		foreach($datafield as $keys) {
			$result=$post[$keys.$fieldzusatz];
			if ($keys=='match_date') $result.=' '.$post['match_time'.$fieldzusatz];
			if ($keys=='matchpart1_result_split' || $keys=='matchpart2_result_split' || $keys=='homeroster' || $keys=='awayroster') {
				$result=trim(join(";",$result));
			} 
			if ($keys=='alt_decision' && $post[$keys.$fieldzusatz]==0) $query.=", matchpart1_result_decision=NULL, matchpart2_result_decision=NULL, decision_info=''";
			if ($keys=='matchpart1_result_decision' && strtoupper($post[$keys.$fieldzusatz])=='X' && $post['alt_decision'.$fieldzusatz]==1) $result='';
			if ($keys=='matchpart2_result_decision' && strtoupper($post[$keys.$fieldzusatz])=='X' && $post['alt_decision'.$fieldzusatz]==1) $result='';
			if (!is_numeric($result)) $vorzeichen = "'"; else $vorzeichen = "";
			if (strstr("homeroster, awayroster, show_report, matchpart1_result, matchpart1_legs, matchpart2_result, matchpart2_legs, matchpart1_result_decision, matchpart2_result_decision, matchpart1_result_split, matchpart2_result_split,", $keys.',') && $result=='' && isset($post[$keys.$fieldzusatz])) {
				$result = 'NULL';
				$vorzeichen = '';	
			}
			if ($keys=='crowd' && $post['crowd'.$fieldzusatz]=='') $result='0';
			if ($result!='' || $keys=='summary' || $keys=='match_result_detail') {
				if ($query) $query.=', ';
				$query.=$keys.'='.$vorzeichen.$result.$vorzeichen;
			}
		}
	}
	$query = 'UPDATE #__joomleague_matches SET '.$query.' WHERE match_id='.$cid;
	$this->_db->setQuery($query);
	$this->_db->query($query);
    return true;
  }
  
  function publish_array( $cid=null, $post=null, $publish=1, $myid=0 ) {
		if (!is_array( $cid ) || count( $cid ) < 1) {
			$this->_error = "No items selected.";
			return false;
		}
		
		$cids = implode( ',', $cid );
		
		$this->_db->setQuery( "UPDATE $this->_tbl SET published='$publish'"
		. "\nWHERE match_id IN ($cids) AND (checked_out=0 OR (checked_out='$myid'))"
		);
    print $this->_db->getQuery();
		if (!$this->_db->query()) {
			$this->_error = $this->_db->getErrorMsg();
			return false;
		}
		
		if (count( $cid ) == 1) {
			$this->checkin( $cid[0] );
		}
		$this->_error = '';
		return $post["id"];
	}
  
  function setNull( $cid=null, $post, $myid=0 ) {
		if (!is_array( $cid ) || count( $cid ) < 1) {
			$this->_error = "No items selected.";
			return false;
		}
		
		$cids = implode( ',', $cid );
// added matchpart1_result_split and matchpart2_result_split to query Gucky 2007/05/25
// removed false ; after decision_info, set a , instead Gucky 2007/05/26
		$this->_db->setQuery( "UPDATE $this->_tbl SET 
                matchpart1_result = NULL, 
                matchpart2_result = NULL,
                matchpart1_result_split = NULL, 
                matchpart2_result_split = NULL,
                matchpart1_legs = NULL,
                matchpart2_legs = NULL, 
                match_result_type = 0,
                matchpart1_result_decision = NULL, 
                matchpart2_result_decision = NULL,
                homeroster = NULL, 
                awayroster = NULL,
                alt_decision = 0,
                decision_info = '',
                crowd = 0,
                referee_id = 0,
							  summary = '',
							  show_report = 0,
							  match_result_detail = '' 
         WHERE match_id IN ($cids) AND (checked_out=0 OR (checked_out='$myid'))"
		);
    print $this->_db->getQuery();
		if (!$this->_db->query()) {
			$this->_error = $this->_db->getErrorMsg();
			return false;
		}
		
		if (count( $cid ) == 1) {
			$this->checkin( $cid[0] );
		}
		$this->_error = '';
		return $post["id"];
	}
  
  function removeMatch( $cid=null ) {
    	if (!is_array( $cid ) || count( $cid ) < 1) {
		$this->_error = "No items selected.";
		return false;
	}
    foreach($cid as $cids) {
	  $query = sprintf("DELETE FROM #__joomleague_match_events WHERE match_id = %s",$cids);
      $this->_db->setQuery($query);
	  $this->_db->query();
      $query = sprintf("DELETE FROM #__joomleague_matches WHERE match_id = %s",$cids);
      $this->_db->setQuery($query);
      $this->_db->query();
    }
    return true;
  }
  
  function getTeams(){
  
    $teams = array();
    
    $team1 = new JL_Team( $this->_db);
    $team1->load($this->matchpart1);
    
    $team2 = new JL_Team( $this->_db);
    $team2->load($this->matchpart2);
    
    $teams[] = $team1;
    $teams[] = $team2;
    
    return $teams;
    
  }
  
}

?>
